﻿using fuzhu;
using LH.Model.PU;
using LH.Model.SA;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; 
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; 
using Quartz.Impl.Triggers;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using XXX.api.Filter;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.PU;
using Yonyou.U8.BO;

namespace XXX.api.Controllers.WDFT
{
    /// <summary>
    /// 请购单
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]
    //[TypeFilter(typeof(ApiLoggingFilterFactory))]
    public class PU_AppVouchController : BaseController
    {

        private readonly ILogger<PU_AppVouchController> _logger;
        //private readonly IEmailService _EmailService;


        public PU_AppVouchController(ILogger<PU_AppVouchController> logger )
        {
            _logger = logger;
            //_EmailService = emailService;
        }


        /// <summary>
        /// 常规对接的请购
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        //[HttpPost]
        //[Route("AddVerify")]
        //public RetModel2 AddVerify(HuaJin_AppVouch obj)
        //{
        //    //string requestBody = DbHelper.GetDbString(obj);
        //    //if (requestBody == null)
        //    //{
        //    //    return DBService.ReErrorObject("入参有误，没有数据");
        //    //}
        //    //HuaJin_AppVouch huaJin_AppVouch = JsonConvert.DeserializeObject<HuaJin_AppVouch>(requestBody);
        //    HuaJin_AppVouch huaJin_AppVouch = obj;
        //    #region 接口调用间隔
        //    //string strKey = ControllerContext.RouteData.Values["controller"].ToString() + Md5Encrypt.MD5(obj.cCode);

        //    //if (CacheHelpCore.GetCacheValue(strKey) == null)
        //    //{
        //    //    CacheHelpCore.SetChacheValue(strKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CanShu.chkRepeat);
        //    //}
        //    //else
        //    //{
        //    //    throw new Exception("接口调用间隔过短");
        //    //}
        //    #endregion 接口调用间隔

        //    try
        //    {
        //        string cErr = "";
        //        string cCode = "";
               
        //        try
        //        {
        //            //huaJin_AppVouch = JsonConvert.DeserializeObject<HuaJin_AppVouch>(requestBody);
        //            if (huaJin_AppVouch == null)
        //            {
        //                cErr = "json结构异常!空值";
        //                return null;
        //            }
        //        }
        //        catch (Exception e)
        //        {
        //            throw new Exception("json结构异常！" + e.Message);
        //        }
        //        if (string.IsNullOrWhiteSpace(huaJin_AppVouch.cCode))
        //        {
        //            cErr = "请购单号不能为空";
        //            return null;
        //        }
        //        PuAppVouchVM model = new PuAppVouchVM(new PuAppVouch.Head(),new List<PuAppVouch.Body>());

        //        model = model.GetEntity(huaJin_AppVouch, ref cErr,ref cCode);
        //        RetModel2 ret;
        //        if (model == null)
        //        {
        //            ret = DBService.ReErrorObject($"入参有误:" + cErr);
        //        }
        //        else
        //        {

                     
        //            ret = model.AddVerify();
        //            //model.conStr = CanShu.conStr;                   

        //        }
                 
        //        // 将 JSON 数据保存到数据库中
        //        //JsonSaveService.saveLog<HuaJin_AppVouch>(huaJin_AppVouch, "请购单", requestBody, ret.code, ret.message);
        //        return ret;
        //    }
        //    catch (Exception ex)
        //    {
        //        //_EmailService.Send(CanShu.mail.ToAddress, CanShu.U8Account + "请购单创建错误,程序错误："  , "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ex.Message);
        //        return DBService.ReErrorObject(ex.Message);
        //    }


             

        //}



        /// <summary>
        /// 直接生成实体的请购
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddEntity")]
        public RetModel2 AddEntity(PuAppVouch obj)
        {
            try
            {
                PuAppVouchVM model = new PuAppVouchVM(new PuAppVouch.PuAppVouchHead(), new List<PuAppVouch.PuAppVouchBody>());
                model.head = obj.head;
                model.bodies = obj.bodies;


                RetModel2 ret = model.Add();

                // 将 JSON 数据保存到数据库中
                //JsonSaveService.saveLog<HuaJin_AppVouch>(huaJin_AppVouch, "请购单", requestBody, ret.code, ret.message);
                return ret;
            }
            catch (Exception ex)
            {
                //_EmailService.Send(CanShu.mail.ToAddress, CanShu.U8Account + "请购单创建错误,程序错误："  , "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ex.Message);
                return DBService.ReErrorObject(ex.Message);
            }




        }




    }
}
